/*
 *@本js功能：学生主页
 *@本js作者：杨锡君
 *@编写时间：2017年11月28日
 */
$(function(){
	var Alert1 = new Alert(); 
	Alert1.init();
	var confirm1 = new Confirm();
	var $courseChose = $("#courseChose");
	var $courseTable = $(".courseTable");
	var $nowWeek = $(".nowWeek");
	var $preWeek = $(".preWeek");
	var $nextWeek = $(".nextWeek");
	var courseArr = [];
	function tableModel(opt){
		var mon = opt[0];
		var monday = mon.split(" ")[1];
		var mondate = mon.split(" ")[0];
		var tue = opt[1];
		var tuesday = tue.split(" ")[1];
		var tuedate = tue.split(" ")[0];
		var wed = opt[2];
		var wednesday = wed.split(" ")[1];
		var weddate = wed.split(" ")[0];
		var thu = opt[3];
		var thursday = thu.split(" ")[1];
		var thudate = thu.split(" ")[0];
		var fri = opt[4];
		var friday = fri.split(" ")[1];
		var fridate = fri.split(" ")[0];
		var sat = opt[5];
		var saturday = sat.split(" ")[1];
		var satdate = sat.split(" ")[0];
		var sun = opt[6];
		var sunday = sun.split(" ")[1];
		var sundate = sun.split(" ")[0];
		return "<tr>"
				+"<th></th>"	
				+"<th>"
				+"<div>"+ monday +"</div>"
				+"<div class=\"nowTime\">"+ mondate +"</div>"
				+"</th>"
				+"<th>"
				+"<div>"+ tuesday +"</div>"
				+"<div>"+ tuedate +"</div>"
				+"</th>"
				+"<th>"
				+"<div>"+ wednesday +"</div>"
				+"<div>"+ weddate +"</div>"
				+"</th>"
				+"<th>"
				+"<div>"+ thursday +"</div>"
				+"<div>"+ thudate +"</div>"
				+"</th>"
				+"<th>"
				+"<div>"+ friday +"</div>"
				+"<div>"+ fridate +"</div>"
				+"</th>"
				+"<th>"
				+"<div>"+ saturday +"</div>"
				+"<div>"+ satdate +"</div>"
				+"</th>"
				+"<th>"
				+"<div>"+ sunday +"</div>"
				+"<div>"+ sundate +"</div>"
				+"</th>"
				+"</tr>"
				+"<tr>"
				+"<td rowspan=\"2\">"
				+"<div class=\"fl\">上&emsp;午</div>"
				+"<div  class=\"fl\">"
				+"<div>第一节</div>"
				+"<div>第二节</div>"
				+"</div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"1\" data-date='"+ mondate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"1\" data-date='"+ tuedate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"1\" data-date='"+ weddate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"1\" data-date='"+ thudate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"1\" data-date='"+ fridate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"1\" data-date='"+ satdate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"1\" data-date='"+ sundate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"</tr>"
				+"<tr>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"2\" data-date='"+ mondate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"2\" data-date='"+ tuedate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"2\" data-date='"+ weddate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"2\" data-date='"+ thudate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"2\" data-date='"+ fridate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"2\" data-date='"+ satdate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"2\" data-date='"+ sundate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"</tr>"
				+"<tr>"
				+"<td rowspan=\"2\">"
				+"<div class=\"fl\">下&emsp;午</div>"
				+"<div  class=\"fl\">"
				+"<div>第三节</div>"
				+"<div>第四节</div>"
				+"</div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"3\" data-date='"+ mondate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"3\" data-date='"+ tuedate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"3\" data-date='"+ weddate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"3\" data-date='"+ thudate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"3\" data-date='"+ fridate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"3\" data-date='"+ satdate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"3\" data-date='"+ sundate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"</tr>"
				+"<tr>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"4\" data-date='"+ mondate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"4\" data-date='"+ tuedate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"4\" data-date='"+ weddate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"4\" data-date='"+ thudate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"4\" data-date='"+ fridate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"4\" data-date='"+ satdate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"4\" data-date='"+ sundate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"</tr>"
				+"<tr>"
				+"<td rowspan=\"2\">"
				+"<div class=\"fl\">晚&emsp;上</div>"
				+"<div  class=\"fl\">"
				+"<div>第五节</div>"
				+"</div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"5\" data-date='"+ mondate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"5\" data-date='"+ tuedate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"5\" data-date='"+ weddate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"5\" data-date='"+ thudate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"5\" data-date='"+ fridate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"5\" data-date='"+ satdate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"5\" data-date='"+ sundate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"</tr>"; 
	}
	function classDataModel(opt){
		var userName = opt.userName;
		var sumSource = opt.myRank.sumSource;
		var rank = opt.myRank.rank;
		var courseTestTime = opt.courseTestTime;
		if(rank == null || rank == undefined){
			rank = 0;
		}
		if(sumSource == null || sumSource == undefined){
			sumSource = 0;
		}
		if(courseTestTime == null || courseTestTime == undefined){
			courseTestTime = 0;
		}
		if(courseTestTime != 0){
            var times =  Math.floor(courseTestTime/3600);
            if(times<1){
            	times = 1;
			}
		}else{
            times = 0;
		}

		return "<p title="+ userName +">任课教师："+ userName +"</p>"
				+"<p>课程得分："+ sumSource +"分</p>"
				+"<p>课程排名：第"+ rank +"名</p>"
				+"<p>实验时长："+ times +"小时</p>";
	}
	var classNoData = "<p>任课教师：--</p>"
        +"<p>课程得分：0分</p>"
        +"<p>课程排名：第0名</p>"
        +"<p>实验时长：0小时</p>";
	var n = 0;
	var teaIndex = {
		init:function(){
			this.bindEvent();
		},
		bindEvent:function(){
			var self = this;
			self.stuData();
			self.getCourseList();
			self.getTableData(timeObject.changedate());
			self.weekTap();
			$(".searchBtn").off("click").click(function(){
                $(".rankBtn").addClass("ranks");
				self.getAllData($courseChose.val());
			});
		},
		getCourseList:function(){
			var self = this;
			$.ajax({
				type:"post",
				url:"stuStudyCourses",
				async:true,
				success:function(msg){
					msg = JSON.parse(msg);
					if(msg.result == "success"){
						if(msg.list.length!=0){
							$.map(msg.list, function(opt) {
								$courseChose.append("<option value="+ opt.courseId +">"+ opt.courseName +"</option>");
							});
							self.getAllData($courseChose.children("option:first").val());	
						}else{
                            $(".infoBox").html(classNoData);
						}
						
					}else{
						Alert1.content = "获取课程失败";
						Alert1.init();
						Alert1.show();
					}
				}
			});
		},
		getAllData:function(courseid){
			var self = this;
			var jsonStr = JSON.stringify({
				"courseId":Number(courseid)
			});
			$.ajax({
				type:"post",
				url:"stuMainCensus",
				async:true,
				data:{
					"jsonStr":jsonStr
				},
				success:function(msg){
                    msg = JSON.parse(msg);
					$(".infoBox").html(classDataModel(msg));
					self.canvasEvent(msg);
				}
			});
		},
		canvasEvent:function(opt){
			var self = this;
					// 基于准备好的dom，初始化echarts图表

			var arr = [];
			$.each(opt.gradeList, function(key) {
				arr.push(this.userName)
			});
			var arrs = [];
			$.each(opt.gradeList, function(key) {
				arrs.push(this.sumSource)
			});
			var rank = opt.myRank.rank;
			var sumSource = opt.myRank.sumSource;
			self.showTable(rank,sumSource,arr,arrs);
            var arrb = [];
            $.each(opt.gradeListIds, function(key) {
                arrb.push(this.userName)
            });
            var arrbs = [];
            $.each(opt.gradeListIds, function(key) {
                arrbs.push(this.sumSource)
            });
            var arrbst = [];
            $.each(opt.gradeListIds, function(key) {
                arrbst.push(this.stuorteaId)
            });
            var ranks = arrbst.indexOf(opt.myRank.stuorteaId)+1;
            var sumSources = opt.myRank.sumSource;
            var n = 0;
            $(".rankBtn").off("click").click(function () {
				if(n%2 == 0){
                    $(".rankBtn").removeClass("ranks");
                    self.showTable(ranks,sumSources,arrb,arrbs);
				}else{
                    $(".rankBtn").addClass("ranks");
                    self.showTable(rank,sumSource,arr,arrs);
				}
				n++;
            });
		},
		showTable:function (rank,sumSource,arr,arrs) {
            var myChart = echarts.init(document.getElementById('svgBox'));
            var option = {
                //			title: {
                //				text: '17级大数据应用1班-期末成绩统计表'
                //			},
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'none'
                    }
                },
                color:["#068ccd"],
                //			legend: {
                //				data: ['排名']
                //			},
                calculable: true,
                xAxis: [{
                    splitLine: {
                        show: false
                    }, //去除网格线
                    type: 'category',
                    boundaryGap: false,
                    name: '姓名',
                    data: arr,
                    axisLabel: {
                        show: true,
                        interval:0,
                        formatter:function(params) {
                            var newParamsName = "";
                            var paramsNameNumber = params.length;
                            var provideNumber = 1;  //一行显示几个字
                            var rowNumber = Math.ceil(paramsNameNumber / provideNumber);
                            if (paramsNameNumber > provideNumber) {
                                for (var p = 0; p < rowNumber; p++) {
                                    var tempStr = "";
                                    var start = p * provideNumber;
                                    var end = start + provideNumber;
                                    if (p == rowNumber - 1) {
                                        tempStr = params.substring(start, paramsNameNumber);
                                    } else {
                                        tempStr = params.substring(start, end) + "\n";
                                    }
                                    newParamsName += tempStr;
                                }

                            } else {
                                newParamsName = params;
                            }
                            return newParamsName
                        },
                        textStyle: {
                            color: '#333' //文字颜色
                        }
                    }
                }],
                yAxis: [{
                    type: 'value',
                    name: '成绩'
                }],
                grid: { // 控制图的大小，调整下面这些值就可以，
                    x: 40, //控制y轴文字的宽度，值越大 间隙越大
                    x2: 50, //控制x轴文字宽度，值越小 间隙越大
                    y2: 100   // y2可以控制 X轴每个控件之间的间隔，值越小 间隙越
                },
                series: [{
                    name: '成绩',
                    type: 'line',
                    smooth: true,
                    itemStyle: {
                        normal: {
                            areaStyle: {
                                type: 'default'
                            }
                        }
                    },
                    data: arrs,
                    markPoint: {
                        data: [{
                            name: '我的成绩',
                            value: sumSource,
                            xAxis: rank-1,
                            yAxis: sumSource
                        } ]
                    }
                }
				]
            };
            // 为echarts对象加载数据
            myChart.setOption(option);
        },
		stuData:function(){
//			获取学生统计
			$.ajax({
				type:"post",
				url:"stuMainCensus",
				async:true,
				success:function(msg){
                    msg = JSON.parse(msg);
					var testTime = msg.testTime;
					if(testTime == null || testTime == undefined){
						testTime = 0;
					}
					var problemNum = msg.problemNum;
					if(problemNum == null || problemNum == undefined){
						problemNum = 0;
					}
					var answerNum = msg.answerNum;
					if(answerNum == null || answerNum == undefined){
						answerNum = 0;
					}
					var homeworkNum = msg.homeworkNum;
					if(homeworkNum == null || homeworkNum == undefined){
						homeworkNum = 0;
					}
					var testNum = msg.testNum;
					if(testNum == null || testNum == undefined){
						testNum = 0;
					}
					if(testTime!=0){
                        var timeLab = Math.floor(testTime/3600);

                        if(timeLab < 1){
                            timeLab = 1;
                        }
                        $(".labTime").html(timeLab+"h");
					}else{
                        $(".labTime").html(0+"h");
					}

					$(".issueNum").html(problemNum);
					$(".answerNum").html(answerNum);
					$(".workNum").html(homeworkNum);
					$(".labWork").html(testNum);
				}
			});
		},
		weekTap:function(){
			var self = this;
//			上下周本周切换
			$nowWeek.off("click").click(function(){
				
				self.getTableData(timeObject.changedate());
				
			});
			$preWeek.off("click").click(function(){
				var date = new Date($(".nowTime").html()).getTime()-7*24*60*60*1000;
				var time = timeObject.changedateYear(date);
				self.getTableData(time);
			});
			$nextWeek.off("click").click(function(){
				var date = new Date($(".nowTime").html()).getTime()+7*24*60*60*1000;
				var time = timeObject.changedateYear(date);
				self.getTableData(time);
			});
		},
		getTableData:function(date){
//			获取已有课程
			var self = this;
			var jsonStr = {
					"date":date
			};	
			$.ajax({
				type:"post",
				url:"getCourseTabAjax",
				async:true,
				data:{
					"jsonStr":JSON.stringify(jsonStr)
				},
				success:function(msg){
                    msg = JSON.parse(msg);
					$courseTable.html(tableModel(msg.dateList));
					if(msg.courseTabList.length != 0){
						$.each(msg.courseTabList, function(opt) {
							var that = this;
							$.each($(".tableAddSection[data-date*='"+ this.classDate +"']"), function(key) {
								if($(this).attr("data-num") == that.classNum){
									$(this).html(that. courseName);
									 // $(this).attr({"data-courseid":that.courseId,"data-value":"2","style":"background:"+that.courseColer,"title":that.courseName + "   " + that.userName + "   " + that.siteName+ "   " +that.startTime+"--"+that.endTime});
                                    if(that.selectMark == 1){
                                        $(this).attr({"data-courseid":that.courseId,"data-value":"2","style":"background:"+that.courseColer,"title":that.courseName+"   " + that.userName + "   " + that.siteName+ "    " +that.startTime+"--"+that.endTime});
                                    }else{
                                        $(this).attr({"data-courseid":that.courseId,"data-value":"2","style":"background:"+that.courseColer+" url(pc/images/ico_optional.png) no-repeat right top","title":that.courseName+ "   " + that.userName  + "   " + that.siteName+ "    " +that.startTime+"--"+that.endTime});
                                    }
								}
							});
						});
					}
				}
			});
			
		}
	};
	teaIndex.init();
});
